package com.oceansoft.core.service.impl;

import org.springframework.stereotype.Service;

import com.oceansoft.core.dao.util.SQLUtil;
import com.oceansoft.core.service.IOrganizationService;

@Service("organizationInfo")
public class IOrganizationInfoServiceImpl implements IOrganizationService {


	@Override
	public void remote2local() { 

		SyncDept(); 
		SyncUser();
		System.out.println("全部导入成功~");
	}
	private void SyncDept(){
		String sql = "";
		SQLUtil util = new SQLUtil();
		
		sql = "delete t_sys_dept_brk";
		util.exec(sql);
		
		sql = "insert into t_sys_dept_brk select * from t_sys_dept";
		util.exec(sql);
		
		
		//判断是否有数据
		sql = "select count(1) from t_sys_dept_lczz";
		int c = util.queryForInt(sql);
		if(c>5000)
		{
			
			sql = "delete t_sys_dept";
			util.exec(sql);
			
			sql = "insert into t_sys_dept select name,parentcode,deep,code,simple,remark,indexcode,dbid,'','','','' from t_sys_dept_lczz t";
			util.exec(sql);
			
			
			sql = "update t_sys_dept  t set t.parentid='320500000000' where t.parentid='3205' ";
			util.exec(sql);
			 
			sql = " update t_sys_dept  t set t.alias='市公安局' where t.deptid='320500000000' ";
			util.exec(sql);
		}
		
	}

	private void SyncUser(){
		String sql = "";
		SQLUtil util = new SQLUtil();
		
		
		
		//备份
		sql = "delete t_sys_user_brk";
		util.exec(sql);
		
		sql = "insert into t_sys_user_brk select * from t_sys_user";
		util.exec(sql);
		
		
		//判断是否有数据
		sql = "select count(1) from t_sys_user_lczz";
		int c = util.queryForInt(sql);
		if(c>10000)
		{
		
			//删除旧数据
			sql = "delete t_sys_user";
			util.exec(sql);
			
			//导入新数据
			sql = "insert into t_sys_user select name,jh,t.sfzh,'d3fce83f84191cbb00a81d7aa4620427af1a6670c56f3f32',t.deptcode,t.zw,1,null,t.phone,t.remark,t.dbid,t.updatetime,'',0,'','',code from t_sys_user_lczz t where ((length(t.user_name) = 6 and t.jh != '无') and substr(jh,1,2)!='00') or jh='zwj'";
			util.exec(sql);
			
			//恢复密码
			sql = "update t_sys_user t set ( password,t.ordernum) = (select password,ordernum from t_sys_user_brk t1 where t1.userid=t.userid) ";
			util.exec(sql);
		}
	}
	
}
